version 11
clear all

set memory 50m
set matsize 800
set more off

use OPS_quantities, clear

gen cl = 10000 * prov_32_id + year      /* cluster variable */
drop if year < 2001                     /* no conflict before 2001 */


cap program drop afghreg
program afghreg
/* varlist: depvar indepvar1 indepvar2, where indepvar1 is casualties
and indepvar2 is a second indepvar which should be lagged in the same
way as the first (non-hostile for example). Control variables that
should not be lagged in the same way as indepvar1 goes into the option
control(). */
  syntax varlist(max=3) [if] [in], [cluster(varname numeric) control(varname numeric)]
  if "`cluster'" != "" {
    local std "cluster(`cluster')"
    local stde "cluster(`cluster') nonest"
  }
  else {
    local std ""
    local stde ""
  }
  tokenize `varlist'   /* makes varlist into the locals `1', `2' etc */
/* `1' is depvar, `2' is first indep var (casualties), `3' is second
indepvar which is to be lagged in the same way as the first
(non-hostile for example). `control' is the argument from control()
and `std' is cluster(clustervar) if cluster(clustervar) is
specified, otherwise blank */
  eststo: quietly xi: xtreg `1' `2' `3' `control' i.year `if', fe `stde'
  eststo: quietly xi: xtreg `1' l.(`2' `3') `control' i.year `if', fe `stde'
  eststo: quietly xi: xtreg `1' l(2).(`2' `3') `control' i.year `if', fe `stde'
  eststo: quietly xi: xtreg `1' l(0/2).(`2' `3') `control' i.year `if', fe `stde'
  estadd FE: *                          /* add FE to table */
  estadd YFE: *                         /* add YFE to table */
end

*** DISTRICT LEVEL ***
eststo clear
afghreg q $hostile, $stderr
esttab using tables_figs/hostile_dc.tex, $out $lab

*** DISTRICT LEVEL WITH NON-HOSTILE ***
eststo clear
afghreg q $hostile $nonhostile, $stderr
esttab using tables_figs/hostile_nh_dc.tex, ///
  $out order($hostile L.$hostile L2.$hostile) ///
  varlabels($hostile "Hostile casualties" L.$hostile "Hostile casualties, lagged" /// 
  L2.$hostile "Hostile casualties, two lags" $nonhostile "Non-hostile casualties" ///
  L.$nonhostile "Non-hostile casualties, lagged" L2.$nonhostile "Non-hostile casualties, two lags") 

*** CONTROLLING FOR OBSERVATION BY SATELLITE ***
eststo clear
eststo: quietly xi: xtreg q i.year satellite, fe $stderr nonest
eststo: quietly xi: xtreg q i.year satellite if year > 2001, fe $stderr nonest
afghreg q $hostile, $stderr control(satellite)
esttab using tables_figs/satellite.tex, $out $lab order($hostile L.$hostile L2.$hostile) 

*** Replicate Table 3 only for satellite districts ***
eststo clear
afghreg q $hostile if satellite, $stderr
esttab using tables_figs/satellite_extra.tex, $out $lab

*** PROVINCE LEVEL ***
eststo clear
afghreg q cas_pv_pos, $stderr 
esttab using tables_figs/hostile_pv.tex, $out $lab

*** INTERACTIONS ***
eststo clear
qui su disttokabul, d
local median=r(p50)
local max=r(max)
gen far_kabul=disttokabul>`median'
gen distancetokabul = disttokabul/`max'
gen inter = $hostile * distancetokabul
gen far_cas=$hostile * far_kabul

eststo: quietly xi: xtreg q $hostile inter i.ye, fe $stderr nonest
eststo: quietly xi: xtreg q l.($hostile inter) i.ye, fe $stderr nonest
eststo: quietly xi: xtreg q l2.($hostile inter) i.ye, fe $stderr nonest
eststo: quietly xi: xtreg q l.($hostile far_cas) i.year, fe $stderr nonest
estadd FE: *
estadd YFE: *

esttab using tables_figs/hostile_inter.tex, ///
  $out $lab order($hostile L.$hostile L2.$hostile inter L.inter L2.inter L.far_cas)

*** GRANGER ***
eststo clear

eststo granger1, title(Opium prod.): quietly xi: xtabond q l.$hostile  i.year, robust
testparm l.$hostile
estadd local FE = "Yes"
estadd scalar Fstat = r(chi2)
estadd scalar pval = r(p)

eststo granger2, title(Casualties): quietly xi: xtabond $hostile  l.q i.year, robust
testparm l.q
estadd local FE = "Yes"
estadd scalar Fstat = r(chi2)
estadd scalar pval = r(p)

estadd YFE: *
esttab granger1 granger2 using tables_figs/granger.tex, ///
  replace booktabs nonotes se starlevels(* 0.10 ** 0.05 *** 0.01) ///
  label drop(LD.q LD.$hostile _Iyear* _cons) order(LD.q LD.$hostile) ///
  stats(Fstat pval FE YFE N, labels("$\chi ^{2}$" "p-value" "District FE" "Year FE" "N" ) ///
  fmt(3 3 0 0 0)) nonumbers mtitles( ) $lab



*** SUMMARY STATISTICS ***
eststo clear
estpost summarize q cas_dc $hostile cas_nh_dc $nonhostile satellite disttokabul, listwise
esttab using tables_figs/summary.tex, replace cells("mean(fmt(2)) sd min max") ///
  nomtitle nonumber booktabs varlabels(q "Opium production" cas_dc "Casualties, district" ///
  $hostile "Casualties dummy, district" cas_nh_dc "Non-hostile casualties, district" ///
  $nonhostile "Non-hostile casualties dummy, district" satellite "Satellite" disttokabul ///
  "Distance to Kabul") align(r r r r)

  
*** ROBUSTNESS --- LOG OPIUM PRODUCTION ***
gen lq = log(q) 
eststo clear
afghreg lq $hostile, $stderr 
esttab using tables_figs/hostile_dc_robust.tex, $out $lab

*** ROBUSTNESS --- STANDARD ERRORS ***
eststo clear
afghreg q $hostile, cluster(prov_32_id)
esttab using tables_figs/hostile_dc_robust_se.tex, $out $lab

*** ROBUSTNESS --- COUNT AND DISTRICT SPECIFIC LINEAR TIME TRENDS ***
eststo clear

eststo: quietly xtreg q l.cas_dc, fe $stderr nonest
eststo: quietly xi: xtreg q l.cas_dc i.year, fe $stderr nonest
eststo: quietly xi: xtreg q l(0/2).cas_dc i.year, fe $stderr nonest
xi i.dist_32_id*year i.year
eststo: quietly xtreg q l.$hostile _IdisX*, fe $stderr nonest
estadd local DLT "Yes"
eststo: quietly xtreg q l.$hostile _Iyear_* _IdisX*, fe $stderr nonest
estadd local DLT "Yes"
eststo: quietly xtreg q l(0/2).$hostile _Iyear_* _IdisX*, fe $stderr nonest
estadd local DLT "Yes"
estadd FE: *
estadd YFE: *

esttab using tables_figs/robustness.tex, $out2 $lab ///
  order(cas_dc L.cas_dc L2.cas_dc $hostile L.$hostile L2.$hostile) ///
  stats(FE YFE DLT r2 N, fmt(0 0 0 3 0) labels("District FE" "Year FE" ///
  "District specific linear trend" "R$^{2}$" "N" ))
